15. Two Functions Same Name

Andy learns about typedef and is reminded to always be suspicious of repeated code.

Note: Elecia and Andy use the word "vector". For now you should think of a vector as something similar to a Python list. So when Elecia says "vector vector float", she is referring to a two-dimensional list (a list of lists) whose elements are floats.

Two Functions Same Name

The following line of code can be used to define an entirely new type called t_grid which is a vector of vectors of floats (for now you can think of vectors as being similar to Python lists).

typedef vector < vector <float> > t_grid; 

Anywhere you would have written vector < vector <float> >, you can now just write t_grid!

In the video below Andy discovered something surprising while translating his histogram filter code from Python to C++. He could have two different functions which each had the same name and this didn't cause any problems.

You can find the code Elecia and Andy discuss below the video.

bool close_enough(float v1, float v2) { 
    if (abs(v2-v1) > 0.0001 ) {
        return false;
    } 
    return true;
}

bool close_enough(vector < vector <float> > g1, vector < vector <float> > g2) {
    int i, j;
    float v1, v2;
    for (i=0; i<g1.size(); i++) {
        for (j=0; j<g1[0].size(); j++) {
            v1 = g1[i][j];
            v2 = g2[i][j];
            if (abs(v2-v1) > 0.0001 ) {
                return false;
            }
        }
    }
    return true;
}

Two Functions Same Name